<template>
	<view class="wrap">
		<u-form :model="dataForm" ref="uForm">
			<u-form-item label-position="left" label="案件类型:" prop="reportTypeName" label-width="20%">
				<text v-if="!dataForm.reportTypeName" @click="typeShow = true;">请选择案件类型</text>
				<text v-else @click="typeShow = true;">{{dataForm.reportTypeName}}</text>
				<u-picker mode="selector" v-model="typeShow" :default-selector="[0]" :range="reportTypeOptions"
					range-key="label" @confirm="typeConfirm"></u-picker>
				<u-icon slot="right" name="arrow-right" @click="typeShow = true;"></u-icon>
			</u-form-item>
			<u-form-item label-position="left" label="报案标题:" prop="title" label-width="20%">
				<u-input :border="true" placeholder="请填写报案标题" v-model="dataForm.title" />
			</u-form-item>
			<u-form-item label-position="left" label="报案详情:" prop="content" label-width="20%">
				<u-input type="textarea" :border="true" placeholder="请填写报案详情" v-model="dataForm.content" />
			</u-form-item>
			<u-form-item label-position="left" label="上传证据:" prop="imgUrl" label-width="20%">
				<u-upload width="160" height="160" :action="uploadUrl" :header="headers" :fileList="fileList"
					@on-success="success" @on-remove="uploadDelete" :deletable="true">
				</u-upload>
			</u-form-item>
		</u-form>
		<u-button @click="submit" type="primary">立即报案</u-button>
	</view>
	</view>
</template>

<script>
	import {
		saveReport
	} from '@/api/police/policeReport.js'
	export default {
		data() {
			return {
				uploadUrl: 'http://127.0.0.1:8181/police-security-system/piclocal/upload/pic',
				headers: {
					token: uni.getStorageSync('token')
				},
				fileList: [],
				dataForm: {
					title: '',
					reportTypeName: '',
					reportType: '',
					content: '',
					imgUrlLists: []
				},
				typeShow: false,
				reportTypeOptions: [{
						label: '刑事案件',
						value: 0
					},
					{
						label: '民事案件',
						value: 1
					}
				]
			}
		},
		onLoad() {
			this.fileList = []
		},
		methods: {
			success(data, index, lists, name) {
				console.log(data.data.imgAddress)
				this.dataForm.imgUrlLists.push(data.data.imgAddress)
				console.log(this.dataForm.operationImageList)
			},
			// 图标删除回调函数
			uploadDelete(index, lists, name) {
				console.log(index)
				this.dataForm.imgUrlLists.splice(index, 1)
			},
			typeConfirm(e) {
				this.dataForm.reportTypeName = this.reportTypeOptions[e].label
				this.dataForm.reportType = this.reportTypeOptions[e].value
			},
			submit() {
				console.log(this.dataForm)
				saveReport(this.dataForm).then(res => {
					if (res.code == 0) {
						uni.showToast({
							title: '报警成功！',
							icon: 'success'
						});
						uni.switchTab({
							url: '/pages/home/home'
						});
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'error'
						});
					}
				})
			}
		}
	};
</script>

<style scoped lang="scss">
	.wrap {
		padding: 30rpx;
	}

	.agreement {
		display: flex;
		align-items: center;
		margin: 40rpx 0;

		.agreement-text {
			padding-left: 8rpx;
			color: $u-tips-color;
		}
	}
</style>